﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.TypeTools;
using SystemCenterSoa.DtoModel.SystemManager;
using SystemCenterSoa.IRepository.SystemManager;
using SystemCenterSoa.PoModel.SystemManager;

namespace SystemCenterSoa.Service.SystemManager
{
    public class WebSystemMenuService
    {
        private IWebSystemRepository _webSystemMenuDal;
        public WebSystemMenuService(IWebSystemRepository webSystemMenuDal)
        {
            _webSystemMenuDal = webSystemMenuDal;
        }
        /// <summary>
        /// 从数据库获取所有模块
        /// </summary>
        /// <returns></returns>
        public List<WebSystemMenuPo> GetWebSystemMenuAll()
        {
            return _webSystemMenuDal.GetWebSystemMenuAll();
        }
        /// <summary>
        /// 从数据库获取所有的模块, 并且初始化为层级模块
        /// </summary>
        /// <returns></returns>
        public List<WebSystemMenuViewDto> GetWebSystemMenuViewsAll()
        {
            List<WebSystemMenuPo> wsmDbs = GetWebSystemMenuAll();
            List<WebSystemMenuViewDto> viewsAll = new List<WebSystemMenuViewDto>();
            ///获取最顶级的列表
            List<WebSystemMenuPo> oneModel = wsmDbs.Where(c => c.wm_level == 1).ToList();

            foreach (var item in oneModel)
            {
                //对子模块进行子模块的子模块列表递归初始化
                WebSystemMenuViewDto oneView = IniViewModel(item, wsmDbs);
                viewsAll.Add(oneView);
            }
            return viewsAll;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<WebSystemMenuViewDto> GetUserWebSystemMenus()
        {
            LoginUserDto user = AdminUserService.GetLoginUser();
            if (user == null) return null;
            List<WebSystemMenuPo> dbMenu = _webSystemMenuDal.GetWebSystemMenusUserName(user.user_name);
            if (dbMenu == null || dbMenu.Count == 0) return null;
            List<WebSystemMenuViewDto> viewsAll = new List<WebSystemMenuViewDto>();
            ///获取最顶级的列表
            List<WebSystemMenuPo> oneModels = dbMenu.Where(c => c.wm_level == 1).ToList();
            foreach (var item in oneModels)
            {
                WebSystemMenuViewDto oneView = IniViewModel(item, dbMenu);
                viewsAll.Add(oneView);
            }
            return viewsAll;
        }

        public WebSystemMenuViewDto GetWebSystemMenuViewByCode(string wMCode)
        {
            return _webSystemMenuDal.GetWebSystemMenuViewByCode(wMCode);
        }

        /// <summary>
        /// 对指定模块进行初始化和递归初始化模块子列表, 并返回初始化好的View
        /// </summary>
        /// <param name="db">指定模块</param>
        /// <param name="wsmDbs">所有模块列表</param>
        /// <returns></returns>
        public WebSystemMenuViewDto IniViewModel(WebSystemMenuPo db, List<WebSystemMenuPo> wsmDbs)
        {
            //对model之间的数据进行复制
            WebSystemMenuViewDto view = ModelCopier.CreateInstanceFrom<WebSystemMenuViewDto>(db);
            //搜索指定菜单的所有的子列表
            List<WebSystemMenuPo> sonList = wsmDbs.Where(c => c.wm_parent_code == view.wm_code).ToList();
            //循环子列表
            if (sonList != null && sonList.Count > 0)
            {
                view.SonWebSystemMenuViewList = new List<WebSystemMenuViewDto>();
                foreach (var s in sonList)
                {
                    //对子模块进行子模块的子模块列表递归初始化
                    WebSystemMenuViewDto son = IniViewModel(s, wsmDbs);
                    view.SonWebSystemMenuViewList.Add(son);
                }
            }

            return view;

        }

        public bool VMenuCode(string wsmCode)
        {
            if (_webSystemMenuDal.GetWebSystemMenuByCode(wsmCode) == null) return true;
            return false;
        }

        public bool MenuAdd(WebSystemMenuPo wsmDb)
        {
            return _webSystemMenuDal.WebSystemMenuAdd(wsmDb);
        }
        public bool MenuUpdate(WebSystemMenuPo wsmDb)
        {
            return _webSystemMenuDal.WebSystemMenuUpdate(wsmDb);
        }

    }
}
